#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

//练习：二分查找


int main()
{
	int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]) - 1;
	int key = 0;//要查找的数字
	int mid = 0;//记录中间元素的下标;
	int flag = 0;
	
	printf("请输入你要查找的元素\n");
	scanf("%d", &key);
	
	while (left <= right)
	{
		mid = left + (right - left) / 2;
		if (arr[mid] > key)
		{
			right = mid - 1;
		}
		else if (arr[mid] < key)
		{
			left = mid + 1;
		}
		else
		{
			flag = 1;
			break;
		}
	}

	if (1 == flag)
	{
		printf("找到了，该元素的下标是%d\n", mid);
	}
	else
	{
		printf("找不到，该数组中无该元素\n");
	}
	return 0;
}